# -*- coding: UTF-8 -*-
import time
from functools import wraps
import pandas as pd
import os
import datetime

g_all = []
end_func_name_list = [""]
time_use_list = []
num = 3
file_name = "time.csv"
use = False


def get_current_time_str():
    time_local = time.localtime()
    res = time.strftime('%Y_%m_%d_%H-%M-%S',time_local)
    return res


def use_time(func):
    @wraps(func)
    def wrap(*args, **kwargs):

        global g_all,num,time_use_list
        time_use = {}
        start = time.time()
        g_all.append(func.__name__)
        res = func(*args, **kwargs)
        end = time.time()
        time_use["use_time"+str(num)] = end-start
        f_name = ":".join(g_all)
        time_use["use_func"+str(num)] = f_name
        time_use_list.append(time_use)
        name = g_all.pop()
        if name in end_func_name_list and use:
            if os.path.isfile(file_name):
                old_df = pd.read_csv(file_name)
                time_use_list.reverse()
                df = pd.DataFrame(time_use_list)
                # df = df.sort_values(by=["use_func" + str(num)],axis=0)
                # df.reset_index(drop=True,inplace=True)
                new_df = pd.concat([old_df, df], axis=1,ignore_index=True)
                new_df.to_csv(file_name,index=False)
            else:
                time_use_list.reverse()
                df = pd.DataFrame(time_use_list)
                # df = df.sort_values(by="use_func" + str(num), axis=0)
                df.to_csv(file_name, index=False)
            num += 1
            time_use_list = []
        time_use = {}
        #print('#FUN(%s) Use time=[%s]'%(func.__name__, end-start))
        # print("time_use_list:",time_use_list)
        # print("num:",num)
        # print("g_all:",g_all)
        return res
    return wrap


def get_term_starttime():
    currentYear = datetime.datetime.now().year
    currentMouth = datetime.datetime.now().month
    currentDay = datetime.datetime.now().day

    if (currentMouth == 2 and currentDay > 15)\
            or (currentMouth > 2 and currentMouth < 9):
        date1 = datetime.date(year=currentYear, month=2, day=15)

    elif currentMouth > 9:
        date1 = datetime.date(year=currentYear, month=9, day=1)

    else:
        date1 = datetime.date(year=currentYear-1, month=9, day=1)

    return date1


def get_current_week():
    monday, sunday = datetime.date.today(), datetime.date.today()
    one_day = datetime.timedelta(days=1)
    while monday.weekday() != 0:
        monday -= one_day
    while sunday.weekday() != 6:
        sunday += one_day
    return monday, sunday


def get_last_week():
    monday, sunday = datetime.date.today(), datetime.date.today()
    one_day = datetime.timedelta(days=1)
    monday -= one_day*7
    sunday -= one_day*7
    while monday.weekday() != 0:
        monday -= one_day
    while sunday.weekday() != 6:
        sunday += one_day
    return monday, sunday

def get_M_S_day(t_day):
    t_day = datetime.datetime.strptime(t_day, "%Y-%m-%d").date()
    print('t_day',t_day)
    one_day = datetime.timedelta(days=1)
    t_day += one_day *7
    monday, sunday = t_day, t_day

    monday -= one_day*7
    sunday -= one_day*7
    while monday.weekday() != 0:
        monday -= one_day
    while sunday.weekday() != 6:
        sunday += one_day
    return monday, sunday

def get_last_year():
    monday, sunday = datetime.date.today(), datetime.date.today()
    one_day = datetime.timedelta(days=1)
    monday -= one_day*7
    sunday -= one_day*7
    while monday.weekday() != 0:
        monday -= one_day
    while sunday.weekday() != 6:
        sunday += one_day
    return monday - one_day*365, sunday


if __name__ == '__main__':
    # print(get_term_starttime())
    # print(get_last_year())

    print(get_M_S_day('2022-01-11'))
# def use_time(func):
#     @wraps(func)
#     def wrap(*args, **kwargs):
#         global g_all
#         start = time.time()
#         res = func(*args, **kwargs)
#         end = time.time()
#         print('#FUN(%s) Use time=[%s]'%(func.__name__, end-start))
#         g_all = _write_excel(g_all,func.__name__,str(end-start),use=True)
#         return res
#     return wrap
#
# def _judge(g,name,s):
#     if name not in g:
#         g[name] = [s]
#     else:
#         g[name] = [g[name][0] + "," + s]
#     return g
#
# def _write_excel(g,name,s,use=False):
#     if use:
#         if name in end_func_name_list:
#             g[name] = [s]
#             time_result = {"time":[json.dumps(g)]}
#             df = pd.DataFrame(time_result)
#             df.to_csv('time.csv', index=False, header=0,mode="a")
#             print(g)
#             g = {}
#         else:
#             g = _judge(g,name,s)
#     else:
#         g = {}
#     return g
